package App.Config;

/**
 * Created by xw on 2019/1/29.
 */

import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
import org.hibernate.SessionFactory;
import org.hibernate.jpa.HibernateEntityManagerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManagerFactory;
import java.util.HashMap;
import java.util.Map;


public class SpringConfig {
    @Autowired
    private EntityManagerFactory entityManagerFactory;

    @Bean
    public SessionFactory getSessionFactory() {
        if (entityManagerFactory.unwrap(SessionFactory.class) == null) {
            throw new NullPointerException("factory is not a hibernate factory");
        }
        return entityManagerFactory.unwrap(SessionFactory.class);
    }


}


/**
 * Created by xw on 2019/1/29.
 */

/*

@Configuration
@ComponentScan("com.cwteam.orm.*")
@EnableTransactionManagement// 开启事务及对注解@Transactional的支持
public class SpringConfig {

    // 实体管理工厂配置

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

        LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
        emf.setDataSource(dataSource());
        emf.setJpaVendorAdapter(hibernateJpaVendorAdapter());
        emf.setPackagesToScan("App.Model");// 替代persistences.xml,对该路径下的@Entity扫描初始化

        Map<String, String> jpsMaps = new HashMap<String, String>();// 实体管理器参数的配置
        jpsMaps.put("hibernate.show_sql", "true");
        jpsMaps.put("hibernate.hbm2ddl.auto", "update");
        jpsMaps.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        emf.setJpaPropertyMap(jpsMaps);
        return emf;
    }

    @Bean
    public BasicDataSource dataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        // dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://10.187.134.81:3306/crm_lifa?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false&allowMultiQueries=true");
        dataSource.setUsername("root");
        dataSource.setPassword("MyNewPass4!");
        dataSource.setDefaultAutoCommit(false);
        return dataSource;
    }


    @Bean
    public JpaVendorAdapter hibernateJpaVendorAdapter() {

        HibernateJpaVendorAdapter jpaVA = new HibernateJpaVendorAdapter();
        jpaVA.setDatabasePlatform("org.hibernate.dialect.MySQLDialect");// MySQL平台指定
        return jpaVA;
    }

    @Bean
    public JpaTransactionManager transactionManager() {

        JpaTransactionManager jpaTm = new JpaTransactionManager();
        jpaTm.setEntityManagerFactory(entityManagerFactory().getObject());
        return jpaTm;

    }

}*/

